<template>
  <div class='carousel-wrap' @mouseenter="stop()" @mouseleave="start()">
    <ul class="carousel-body">
      <li class="carousel-item" v-for="(item,i) in sliders" :key="i" :class="{fade:index===i}">
        <!-- 图片 -->
        <RouterLink v-if="item.image_url" :to="item.link">
          <img :src="global.FILE_DOMAIN+item.image_url" alt=""/>
        </RouterLink>

        <!-- 商品列表 -->
        <div v-else class="slider">
            <div class="goods-item-wrap" v-for="goods in item" :key="goods.id" @click="$router.push({path: `/goods-detail`, query:{id:goods.goods_id}})">
              <div class="img-wrap">
                <img :src="global.FILE_DOMAIN+goods.cover_url" alt="">
              </div>
              <div class="name ellipsis-2">{{goods.goods_name}}</div>
              <div class="price-box">
                <p class="price">&yen;{{(goods.price/100).toFixed(2)}}</p>
                <p class="market-price">&yen;{{(goods.market_price
/100).toFixed(2)}}</p>
              </div>
            </div>
        </div>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="toggle(-1)">
      <i class="iconfont icon-zuojiantou"></i>
    </a>
    <a href="javascript:;" class="carousel-btn next" @click="toggle(1)">
      <i class="iconfont icon-xiangyoujiantou"></i>
    </a>
    <div class="carousel-indicator">
      <span v-for="(item,i) in sliders" :key="i" :class="{active:index===i}"></span>
    </div>
  </div>
</template>

<script setup>
import { ref,inject,watch,onUnmounted } from 'vue'

// 获取全局对象
const global = inject('global')


const props = defineProps({
  sliders: {
    type: Array,
    default: () => []
  },
  duration: {
    type: Number,
    default: 3000
  },
  autoPlay: {
    type: Boolean,
    default: false
  }
})

// 默认显示的图片的索引
const index = ref(0)

// 自动播放
let timer = null
const autoPlayFn = () => {
  clearInterval(timer)
  timer = setInterval(() => {
    index.value++
    if (index.value >= props.sliders.length) {
      index.value = 0
    }
  }, props.duration)
}

onUnmounted(() => {
  clearInterval(timer)
})

watch(() => props.sliders, (newVal) => {
  // 有数据&开启自动播放，才调用自动播放函数
  if (newVal.length && props.autoPlay) {
    index.value = 0
      autoPlayFn()
    }
  }, { immediate: true })

const stop = () => {
  if (timer) clearInterval(timer)
}
const start = () => {
  if (props.sliders.length && props.autoPlay) {
    autoPlayFn()
  }
}

const toggle = (step) => {
  const newIndex = index.value + step
  if (newIndex >= props.sliders.length) {
    index.value = 0
    return
  }
  if (newIndex < 0) {
    index.value = props.sliders.length - 1
    return
  }
  index.value = newIndex
}

</script>
  
<style scoped lang="scss">
.carousel-wrap {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel-body {
    width: 100%;
    height: 100%;
    .carousel-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      img {
        width: 100%;
        height: 100%;
      }
      // 轮播商品
      .slider {
        display: flex;
        // justify-content: space-around;
        padding: 0 60px;
        .goods-item-wrap {
          cursor: pointer;
          border: 1px solid #ccc;
          width: 240px;
          padding: 10px;
          margin: 0 20px;
          border-radius: 10px;
          &:nth-child(4n) {
            margin-right: 0;
          }
          .img-wrap {
            width: 220px!important;
            height: 220px!important;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .name {
            font-size: 16px;
            color: #666;
            line-height: 19px;
            margin-top: 10px;
          }
          .price-box {
            display: flex;
            justify-content: space-between;
            margin-top: 15px;
            .price {
              font-size: 18px;
              color: $price-color;
            }
            .market-price {
              text-decoration: line-through;
              color: #ccc;
            }
          }
          &:hover {
            border: 1px solid $main-color;
            transform: scale(1.01);
          }
        }
      }
    }
    .fade {
      opacity: 1;
      z-index: 1;
    }
  }
  .prev{
    left: 20px;
  }
  .carousel-btn {
    width: 44px;
    height: 44px;
    background: rgba(0,0,0,.2);
    color: #fff;
    border-radius: 50%;
    position: absolute;
    top: 228px;
    z-index: 2;
    text-align: center;
    line-height: 44px;
    opacity: 0;
    transition: all 0.5s;
  }
  .next{
    right: 20px;
  }
  .carousel-indicator {
    position: absolute;
    left: 0;
    bottom: 20px;
    z-index: 2;
    width: 100%;
    text-align: center;
    span {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: rgba(0,0,0,0.2);
      border-radius: 50%;
      cursor: pointer;
      ~ span {
        margin-left: 12px;
      }
    }
    .active {
      background: $main-color;
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>